//
//  TGInfoHeaderView.m
//  GroupBuy
//
//  Created by wwj on 15/11/16.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGInfoHeaderView.h"
#import "UIImageView+WebCache.h"
#import "TGDealModel.h"
#import "NSDate+Extend.h"
#import "NSString+Extend.h"

@interface TGInfoHeaderView ()

@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (weak, nonatomic) IBOutlet UIButton *anyTimeBack;
@property (weak, nonatomic) IBOutlet UIButton *expireBack;
@property (weak, nonatomic) IBOutlet UIButton *time;
@property (weak, nonatomic) IBOutlet UIButton *purchaseCount;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textH;
@property (weak, nonatomic) IBOutlet UIView *buttomView;
@end

@implementation TGInfoHeaderView

+(instancetype)infoHeaderView{
    return [[NSBundle mainBundle] loadNibNamed:@"TGInfoHeaderView" owner:nil options:nil][0];
}

-(void)setModel:(TGDealModel *)model{
    _model = model;
    
    if (model.restrictions) { // 有约束（完整的数据）
        // 1.设置是否支持退款
        _anyTimeBack.enabled = model.restrictions.is_refundable;
        _expireBack.enabled = _anyTimeBack.enabled;
    } else { // 不完整的数据
        // 2.下载图片
        [_image sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"placeholder_deal.png"]];
        
        [_time setTitle:model.purchase_deadline forState:UIControlStateNormal];
    }
    
    // 4.购买人数
    NSString *pc = [NSString stringWithFormat:@"%d 人已购买", model.purchase_count];
    [_purchaseCount setTitle:pc forState:UIControlStateNormal];
    
    // 5.设置描述
    _desc.text = model.desc;
    // 描述的高度
    
   CGFloat height = [model.desc sizeWithFont:[UIFont systemFontOfSize:16] maxSize:CGSizeMake(_desc.frame.size.width, MAXFLOAT)].height;
    _textH.constant = height;
   
}

-(void)layoutSubviews{
    CGRect frame = self.frame;
    frame.size.height = CGRectGetMaxY(_buttomView.frame);
    self.frame = frame;
}

@end
